package org.example.ai_chat.controller;

import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.memory.ChatMemory.CONVERSATION_ID;

/**
 * mysql记忆存储
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/chat")
public class MysqlMemoryController {

    @Qualifier("selfChatClient")
    private final ChatClient selfChatClient;
    /**
     * http://localhost:19000/chat/self/memory/chat?
     */
    @GetMapping("/self/memory/chat")
    public Flux<String> selfMemoryChat(@RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query,
                                   @RequestParam(value = "conversationId", defaultValue = "1") String conversationId,
                                   HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");
        return selfChatClient.prompt(query)
                .advisors(
                        a -> a.param(CONVERSATION_ID, conversationId)
                )
                .stream()
                .content();
    }


}
